#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import json
import re
import os
from bs4 import BeautifulSoup

def analyze_preloaded_state():
    """分析HTML文件中的__PRELOADED_STATE__数据结构"""
    
    # HTML文件路径
    html_file = r"C:\Users\byan\Desktop\crawl_by_trae\crawl_by_trae\crawl\html\sciencedirect_www_sciencedirect_com_S1385894725059832_20251027_224410.html"
    
    if not os.path.exists(html_file):
        print(f"错误：HTML文件不存在 {html_file}")
        return
    
    # 读取HTML内容
    try:
        with open(html_file, 'r', encoding='utf-8') as f:
            html_content = f.read()
        print(f"成功读取HTML文件，内容长度: {len(html_content)} 字符")
    except Exception as e:
        print(f"读取HTML文件失败: {e}")
        return
    
    # 解析HTML
    soup = BeautifulSoup(html_content, 'html.parser')
    
    # 查找__PRELOADED_STATE__
    print("\n" + "="*60)
    print("分析 __PRELOADED_STATE__ 数据结构")
    print("="*60)
    
    script_tag = soup.find('script', string=re.compile(r'window\.__PRELOADED_STATE__'))
    if not script_tag:
        print("❌ 未找到包含 __PRELOADED_STATE__ 的script标签")
        return
    
    print("✅ 找到 __PRELOADED_STATE__ script标签")
    
    # 提取JSON数据
    json_str_match = re.search(r'window\.__PRELOADED_STATE__\s*=\s*(.*);', script_tag.string, re.DOTALL)
    if not json_str_match:
        print("❌ 无法通过正则表达式提取JSON数据")
        return
    
    json_str = json_str_match.group(1)
    print(f"✅ 提取到JSON字符串，长度: {len(json_str)} 字符")
    
    try:
        preloaded_state = json.loads(json_str)
        print("✅ 成功解析JSON数据")
    except json.JSONDecodeError as e:
        print(f"❌ JSON解析失败: {e}")
        return
    
    # 分析数据结构
    print("\n" + "-"*40)
    print("数据结构分析:")
    print("-"*40)
    
    # 顶级键
    print(f"顶级键: {list(preloaded_state.keys())}")
    
    # 检查authors键
    if 'authors' in preloaded_state:
        authors_data = preloaded_state['authors']
        print(f"\nauthors键存在，类型: {type(authors_data)}")
        
        if isinstance(authors_data, dict):
            print(f"authors子键: {list(authors_data.keys())}")
            
            # 检查content键
            if 'content' in authors_data:
                content_data = authors_data['content']
                print(f"\nauthors.content存在，类型: {type(content_data)}")
                
                if isinstance(content_data, list):
                    print(f"authors.content长度: {len(content_data)}")
                    
                    if len(content_data) > 0:
                        first_item = content_data[0]
                        print(f"authors.content[0]类型: {type(first_item)}")
                        
                        if isinstance(first_item, dict):
                            print(f"authors.content[0]键: {list(first_item.keys())}")
                            
                            # 检查$$键
                            if '$$' in first_item:
                                dollar_data = first_item['$$']
                                print(f"\nauthors.content[0].$$存在，类型: {type(dollar_data)}")
                                
                                if isinstance(dollar_data, list):
                                    print(f"authors.content[0].$$长度: {len(dollar_data)}")
                                    
                                    # 分析前几个元素
                                    for i, item in enumerate(dollar_data[:3]):
                                        print(f"\n  元素[{i}]:")
                                        if isinstance(item, dict):
                                            print(f"    键: {list(item.keys())}")
                                            if '#name' in item:
                                                print(f"    #name: {item['#name']}")
                                            if '$$' in item:
                                                sub_items = item['$$']
                                                if isinstance(sub_items, list):
                                                    print(f"    子元素数量: {len(sub_items)}")
                                                    for j, sub_item in enumerate(sub_items[:2]):
                                                        if isinstance(sub_item, dict) and '#name' in sub_item:
                                                            print(f"      子元素[{j}] #name: {sub_item['#name']}")
                                        else:
                                            print(f"    类型: {type(item)}")
                            else:
                                print("❌ authors.content[0]中没有$$键")
                        else:
                            print("❌ authors.content[0]不是字典类型")
                    else:
                        print("❌ authors.content为空列表")
                else:
                    print("❌ authors.content不是列表类型")
            else:
                print("❌ authors中没有content键")
        else:
            print("❌ authors不是字典类型")
    else:
        print("❌ 顶级数据中没有authors键")
    
    # 保存完整的数据结构到文件供进一步分析
    try:
        with open('preloaded_state_analysis.json', 'w', encoding='utf-8') as f:
            json.dump(preloaded_state, f, indent=2, ensure_ascii=False)
        print(f"\n✅ 完整数据结构已保存到 preloaded_state_analysis.json")
    except Exception as e:
        print(f"❌ 保存数据结构失败: {e}")

if __name__ == "__main__":
    analyze_preloaded_state()